﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using System.Data;
using Catering.View_Operaction;

namespace Catering.Catering_Page.ShanShiFenXi
{
    public partial class IntroduceNutrition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();

            }
        }

        private void loadData()
        {
            if (TreeView1 != null && TreeView1.Nodes.Count > 0)
            {
                TreeView1.Nodes.Clear();
            }
            Catering_Rules.IntroduceNutrition iNRule = new Catering_Rules.IntroduceNutrition();
            DataTable dtIN = iNRule.GetAll();
            Catering_Rules.NutritionType nTRule = new Catering_Rules.NutritionType();
            DataTable dtNT = nTRule.GetAll();
            DropDownList1.Items.Clear();
            ListItem qxz = new ListItem() { Text = "请选择", Value = "请选择" };
            DropDownList1.Items.Add(qxz);
            for (int i = 0; i < dtNT.Rows.Count; i++)
            {
                ListItem lI = new ListItem() { Text = dtNT.Rows[i]["nutrition"].ToString(), Value = dtNT.Rows[i]["id"].ToString() };
                DropDownList1.Items.Add(lI);
            }
            for (int i = 0; i < dtNT.Rows.Count; i++)
            {
                TreeNode temp = new TreeNode() { Text = dtNT.Rows[i]["nutrition"].ToString(), Value = dtNT.Rows[i]["id"].ToString() };
                for (int ii = 0; ii < dtIN.Rows.Count; ii++)
                {
                    if (Int32.Parse(dtIN.Rows[ii]["typeId"].ToString()) == Int32.Parse(dtNT.Rows[i]["id"].ToString()))
                    {
                        TreeNode cTemp = new TreeNode() { Text = dtIN.Rows[ii]["title"].ToString(), Value = dtIN.Rows[ii]["id"].ToString() };
                        cTemp.Text = dtIN.Rows[ii]["title"].ToString().Length > 9 ? dtIN.Rows[ii]["title"].ToString().Substring(0, 8) + "..." : dtIN.Rows[ii]["title"].ToString().ToString();
                        cTemp.ToolTip = dtIN.Rows[ii]["title"].ToString();
                        temp.ChildNodes.Add(cTemp);
                    }
                }
                TreeView1.Nodes.Add(temp);
            }
        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            txtTitle.Text = "";
            div_content.InnerHtml = "";
            TreeNode temp = TreeView1.SelectedNode;
            if (temp != null && !string.IsNullOrEmpty(temp.Value))
            {
                if (temp.Depth == 0)
                {
                    return;
                }
                Catering_Rules.IntroduceNutrition INRules = new Catering_Rules.IntroduceNutrition();
                DataRow dr = INRules.GetINByID(Int32.Parse(temp.Value));
                txtTitle.Text = dr["title"].ToString();
                div_content.InnerHtml = dr["cText"].ToString();
                txt_div_Title.Text = dr["title"].ToString();
                txtContent.Value = dr["cText"].ToString();
                HD_ReadID.Value = temp.Value;
                int typeid = Convert.ToInt32(dr["typeId"].ToString());
                for (int i = 1; i < DropDownList1.Items.Count; i++)
                {
                    int itemid = Convert.ToInt32(DropDownList1.Items[i].Value);
                    if (itemid == typeid)
                    {
                        DropDownList1.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string strid = HD_Edit.Value;
            int id = 0;
            Int32.TryParse(strid, out id);
            int itemid = 0;
            Catering_Rules.IntroduceNutrition INRules = new Catering_Rules.IntroduceNutrition();

            if (DropDownList1.SelectedItem != null && !DropDownList1.SelectedItem.Value.Equals("请选择"))
            {
                itemid = Convert.ToInt32(DropDownList1.SelectedItem.Value);
            }
            if(string.IsNullOrEmpty(txt_div_Title.Text))
            {
                lblMes.Text = "标题不能为空，操作失败！";
                return;
            }
            if (id == 0 && itemid == 0)
            {
                if (!INRules.ISExistNutritionType(txt_div_Title.Text.Trim()))
                {
                    if (INRules.AddNutritionType(txt_div_Title.Text.Trim()))
                    {
                        loadData();
                        lblMes.Text = "操作成功！";
                    }
                }
                else
                {
                    lblMes.Text = "此分类已存在，操作失败！";
                }
            }
            else if (id > 0 && itemid > 0)
            {
                if (string.IsNullOrEmpty(txtContent.Value))
                {
                    lblMes.Text = "内容不能为空，操作失败！";
                    return;
                }
                if (INRules.UpdateIN(txt_div_Title.Text, txtContent.Value, id, itemid))
                {
                    BindTreeView();
                    TreeView1_SelectedNodeChanged(new object(), null);
                    lblMes.Text = "操作成功！";
                }
            }
            else if (id == 0 && itemid > 0)
            {
                if (string.IsNullOrEmpty(txtContent.Value))
                {
                    lblMes.Text = "内容不能为空，操作失败！";
                    return;
                }
                Catering_Model.IntroduceNutritionDBInfo INInfo = new Catering_Model.IntroduceNutritionDBInfo();
                INInfo.Title = txt_div_Title.Text;
                INInfo.CText = txtContent.Value;
                INInfo.DTime = DateTime.Now;
                INInfo.Typeid = itemid;
                if (INRules.AddIN(INInfo))
                {
                    BindTreeView();
                    lblMes.Text = "操作成功！";
                }
            }
        }
        private void BindTreeView()
        {
            if (TreeView1 != null && TreeView1.Nodes.Count > 0)
            {
                TreeView1.Nodes.Clear();
            }
            Catering_Rules.IntroduceNutrition iNRule = new Catering_Rules.IntroduceNutrition();
            DataTable dtIN = iNRule.GetAll();
            Catering_Rules.NutritionType nTRule = new Catering_Rules.NutritionType();
            DataTable dtNT = nTRule.GetAll();
            for (int i = 0; i < dtNT.Rows.Count; i++)
            {
                TreeNode temp = new TreeNode() { Text = dtNT.Rows[i]["nutrition"].ToString(), Value = dtNT.Rows[i]["id"].ToString() };
                for (int ii = 0; ii < dtIN.Rows.Count; ii++)
                {
                    if (Int32.Parse(dtIN.Rows[ii]["typeId"].ToString()) == Int32.Parse(dtNT.Rows[i]["id"].ToString()))
                    {
                        TreeNode cTemp = new TreeNode() { Text = dtIN.Rows[ii]["title"].ToString(), Value = dtIN.Rows[ii]["id"].ToString() };
                        cTemp.Text = dtIN.Rows[ii]["title"].ToString().Length > 9 ? dtIN.Rows[ii]["title"].ToString().Substring(0, 8) + "..." : dtIN.Rows[ii]["title"].ToString().ToString();
                        cTemp.ToolTip = dtIN.Rows[ii]["title"].ToString();
                        temp.ChildNodes.Add(cTemp);
                    }
                }
                TreeView1.Nodes.Add(temp);
            }
        }
        //protected void Button2_Click(object sender, EventArgs e)
        //{
        //    if (TreeView1.SelectedNode == null || string.IsNullOrEmpty(TreeView1.SelectedNode.Value) || TreeView1.SelectedNode.Depth != 1 || string.IsNullOrEmpty(txtTitle.Text))
        //    {
        //        return;
        //    }
        //    Catering_Rules.IntroduceNutrition INRules = new Catering_Rules.IntroduceNutrition();
        //    if (INRules.UpdateIN(txtTitle.Text, txtContent.Text,Int32.Parse(TreeView1.SelectedNode.Value)))
        //    {
        //        Response.Write(@"<script>alert('提交成功！')</script>");
        //    }
        //}

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSearch.Text))
            {
                return;
            }
            for (int i = 0; i < TreeView1.Nodes.Count; i++)
            {
                for (int ii = 0; ii < TreeView1.Nodes[i].ChildNodes.Count; ii++)
                {
                    if (TreeView1.Nodes[i].ChildNodes[ii].Text.Contains(txtSearch.Text))
                    {
                        TreeView1.Nodes[i].ChildNodes[ii].Selected = true;
                        break;
                    }
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (TreeView1.SelectedNode == null || string.IsNullOrEmpty(TreeView1.SelectedNode.Value))
            {
                return;
            }
            Catering_Rules.IntroduceNutrition INRules = new Catering_Rules.IntroduceNutrition();
            string itemId = TreeView1.SelectedNode.Value;
            if (TreeView1.SelectedNode.Depth == 1)
            {
                if (INRules.DelIN(Int32.Parse(itemId)))
                {
                    //ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "alert('提交成功！');");
                    loadData();
                }
            }
            else
            {
                if (INRules.DeleteAllINByTypeId(itemId) > 0 && INRules.DelNutritionType(itemId) > 0)
                {
                    loadData();
                }
            }
        }
    }
}